package cn.edu.zafu.bigdata.ljs;

import java.io.*;
import java.util.*;

public class StudentManagementSystem {
    private static final String FILE_PATH = "students.dat";
    private Map<String, Student> students = new HashMap<>();

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        system.run();
    }

    public void run() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    retrieveStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    System.out.println("退出程序");
                    saveStudents();
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    private void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (students.containsKey(id)) {
            System.out.println("学号重复，请重新输入！");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.put(id, student);
        saveStudents();
        System.out.println("学生信息添加成功！");
    }

    private void retrieveStudent(Scanner scanner) {
        System.out.print("请输入学号进行检索: ");
        String id = scanner.nextLine();
        if ("QUIT".equalsIgnoreCase(id)) {
            return;
        }
        Student student = students.get(id);
        if (student != null) {
            System.out.println(student);
        } else {
            System.out.println("未找到该学生");
        }
    }

    private void updateStudent(Scanner scanner) {
        System.out.print("请输入学号进行更新: ");
        String id = scanner.nextLine();
        if ("QUIT".equalsIgnoreCase(id)) {
            return;
        }
        Student student = students.get(id);
        if (student == null) {
            System.out.println("未找到该学生");
            return;
        }
        System.out.print("请输入新姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入新电话: ");
        String phone = scanner.nextLine();
        student.setName(name);
        student.setPhone(phone);
        saveStudents();
        System.out.println("学生信息更新成功！");
    }

    private void deleteStudent(Scanner scanner) {
        System.out.print("请输入学号进行删除: ");
        String id = scanner.nextLine();
        if ("QUIT".equalsIgnoreCase(id)) {
            return;
        }
        if (students.remove(id) != null) {
            saveStudents();
            System.out.println("学生信息删除成功！");
        } else {
            System.out.println("未找到该学生");
        }
    }

    private void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_PATH))) {
            students = (Map<String, Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_PATH))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

